package com.acdc.mqtt.config;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * MQTT配置类
 */
@Configuration
@PropertySource("classpath:mqtt.properties")
public class MqttConfig {
    
    private static final Logger log = LoggerFactory.getLogger(MqttConfig.class);
    
    @Value("${mqtt.broker.url:tcp://localhost:1883}")
    private String brokerUrl;
    
    @Value("${mqtt.username:admin}")
    private String username;
    
    @Value("${mqtt.password:public}")
    private String password;
    
    @Value("${mqtt.connection.timeout:30}")
    private int connectionTimeout;
    
    @Value("${mqtt.keep.alive.interval:60}")
    private int keepAliveInterval;
    
    @Value("${mqtt.clean.session:true}")
    private boolean cleanSession;
    
    @Value("${mqtt.automatic.reconnect:true}")
    private boolean automaticReconnect;
    
    @Value("${mqtt.root_topic}")
    private String rootTopic;
    
    /**
     * 创建生产者MQTT连接选项
     */
    @Bean("producerMqttConnectOptions")
    public MqttConnectOptions producerMqttConnectOptions() {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(true); // 生产者使用clean session
        options.setConnectionTimeout(connectionTimeout);
        options.setKeepAliveInterval(keepAliveInterval);
        options.setAutomaticReconnect(automaticReconnect);
        options.setUserName(username);
        options.setPassword(password.toCharArray());
        return options;
    }
    
    /**
     * 创建消费者MQTT连接选项
     */
    @Bean("consumerMqttConnectOptions")
    public MqttConnectOptions consumerMqttConnectOptions() {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(false); // 消费者不使用clean session，保持订阅
        options.setConnectionTimeout(connectionTimeout);
        options.setKeepAliveInterval(keepAliveInterval);
        options.setAutomaticReconnect(automaticReconnect);
        options.setUserName(username);
        options.setPassword(password.toCharArray());
        return options;
    }
    
    /**
     * 创建生产者MQTT客户端
     */
    @Bean("producerMqttClient")
    public MqttClient producerMqttClient() throws MqttException {
        String clientId = "dlt698_producer_" + System.currentTimeMillis();
        MqttClient client = new MqttClient(brokerUrl, clientId, new MemoryPersistence());
        log.info("创建生产者MQTT客户端: clientId={}, brokerUrl={}", clientId, brokerUrl);
        return client;
    }
    
    /**
     * 创建消费者MQTT客户端
     */
    @Bean("consumerMqttClient")
    public MqttClient consumerMqttClient() throws MqttException {
        String clientId = "dlt698_consumer_" + System.currentTimeMillis();
        MqttClient client = new MqttClient(brokerUrl, clientId, new MemoryPersistence());
        log.info("创建消费者MQTT客户端: clientId={}, brokerUrl={}", clientId, brokerUrl);
        return client;
    }

    @Bean
    public String rootTopic() {
        return rootTopic;
    }
} 